package com.wn.ziroom.controller;


import com.wn.utils.CookieUtils;
import com.wn.utils.JwtUtils;
import com.wn.ziroom.entity.Cart;
import com.wn.ziroom.entity.CartItem;
import com.wn.ziroom.entity.User;
import com.wn.ziroom.entity.vo.ProductionVo;
import com.wn.ziroom.service.CartService;
import com.wn.ziroom.service.ProductionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-23 02:27:54
 */
@Controller
@RequestMapping("/ziroom_portal/cart")
public class CartController {

    private CartService cartService;
    private ProductionService productionService;

    @Autowired
    public void setProductionService(ProductionService productionService) {
        this.productionService = productionService;
    }

    @Autowired
    public void setCartService(CartService cartService) {
        this.cartService = cartService;
    }

    @ResponseBody
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("productionId") Integer productionId,
                            @RequestParam(value = "buyCount", required = true, defaultValue = "1") Integer buyCount,
                            HttpServletRequest request){

        ProductionVo production = productionService.findProductionById(productionId);

        String token = CookieUtils.getCookie("token", request);
        if (Objects.isNull(token)){
            return "no";
        }else {
            User user = JwtUtils.verifyTokenWithoutVerifyExp(token, User.class);
            Integer userId = user.getId();
            int i = cartService.addProduction(userId,production,buyCount);
            return "ok";
        }

    }

    @GetMapping("/userCart")
    public ModelAndView userCart(HttpServletRequest request){
        ModelAndView mav = new ModelAndView();
        String token = CookieUtils.getCookie("token", request);
        if (Objects.isNull(token)){
            mav.setViewName("register");
        }else {
            User user = JwtUtils.verifyTokenWithoutVerifyExp(token, User.class);
            Integer userId = user.getId();
            List<CartItem> cartItemList = cartService.findCartByUserId(userId);
            mav.addObject("cartItemList",cartItemList);
            mav.setViewName("cart");
        }
        return mav;
    }

    @ResponseBody
    @GetMapping("/deleteByProductionId")
    public String deleteByProductionId(@RequestParam("productionId") Integer productionId,HttpServletRequest request){
        String token = CookieUtils.getCookie("token", request);
        User user = JwtUtils.verifyTokenWithoutVerifyExp(token, User.class);
        Integer userId = user.getId();
        cartService.deleteProduction(userId,productionId);
        return "ok";
    }

    @ResponseBody
    @GetMapping("/changeBuyCount")
    public String changeBuyCount(@RequestParam("buyCount") Integer buyCount,
                                 @RequestParam("productionId") Integer productionId,
                                 HttpServletRequest request){

        System.out.println("buyCount------->"+buyCount);
        System.out.println("productionId------------>"+productionId);
        if (buyCount <= 0){
            return "no";
        }else {
            String token = CookieUtils.getCookie("token", request);
            User user = JwtUtils.verifyTokenWithoutVerifyExp(token, User.class);
            Integer userId = user.getId();
            cartService.updateBuyCount(userId,buyCount,productionId);
            return "ok";
        }
    }
}

